<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/26 15:32
 * Desc: 系统
 */
class Service_Api_System extends Contract_Api_System
{

    public function app($params): array
    {
        $apps = Model_Api_v2_System::get_xcx_auth($params['mini_type'], $params['siteId'], $params['secret']);

        $product_type_id = Model_Api_v2_System::get_xcx_product_typeid($apps);

        // 模块应用
        $models     = array();
        $model_list = DB::select(
            'model.id',
            'model.modulename',
            'model.pinyin',
            'model.isopen',
            'm_nav.m_title'
        )
            ->from('model')
            ->join('m_nav', 'left')
            ->on('model.id', '=', DB::expr('sline_m_nav.m_typeid'))
            ->where('model.isopen', '=', 1)
            ->where('m_nav.m_isopen', '=', 1)
            ->execute()
            ->as_array();

        foreach ($model_list as $item) {
            $models[$item['id']] = array(
                'name' => $item['modulename'],
                'pinyin' => $item['pinyin'],
                'isopen' => $item['isopen'],
                'title' => $item['m_title'] ?: $item['modulename']
            );
        }

        /*通用模块*/
        $tongyong = DB::select('id', 'modulename')
            ->from("model")
            ->where('maintable', '=', 'model_archive')
            ->execute()
            ->as_array('id', 'modulename');

        // 全局搜索词
        $hot_search_default = Model_Global_Search::get_hot_search_default();

        return array(
            'app' => $apps,
            'product_type_id' => $product_type_id,
            'hot_search_default' => $hot_search_default ?: '',
            'models' => $models,
            'tongyong' => $tongyong
        );
    }

    public function agreement($params): array
    {
        switch ($params['tag']) {
            case 'login':
                $temp = DB::select()->from('sysconfig')
                    ->and_where_open()->where('webid', '=', 0)
                    ->where('varname', 'in', array('cfg_member_agreement_title', 'cfg_member_agreement'))
                    ->and_where_close()
                    ->execute()
                    ->as_array();
                $data = array_reduce($temp, function ($res, $item) {
                    $res[str_replace('cfg_member_', '', $item['varname'])] = $item['value'];
                    return $res;
                }, array());
                break;
            default:
                throw new Exception('tag undefined');
        }

        return $data;
    }

    public function sys_configs($params): array
    {
        $filed  = $params['filed'];
        $fileds = explode(',', $filed);

        $configs = Model_Sysconfig::get_configs(0, $fileds);
        foreach ($configs as $k => $v) {
            if (strpos($k,'order_agreement') !== false) {
                $a           = Model_Api_V2_Xcx::filter_content($v);
                $configs[$k] = $a;
            }
        }
        //微信客服字段判断
        if (!isset($configs['cfg_m_wap_link'])) {
            $configs['cfg_m_wap_link'] = '';
        }
        if (!isset($configs['cfg_m_wap_en_id'])) {
            $configs['cfg_m_wap_en_id'] = '';
        }

        return $configs;
    }

    public function new_ads($params): array
    {
        $prefix       = Arr::get($params, 'prefix');
        $client_no    = Arr::get($params, 'client_no');
        $number       = Arr::get($params, 'number');
        $custom_label = Arr::get($params, 'custom_label', '');

        $builder = DB::select(DB::expr('distinct sline_xcx_ads.*'))
            ->from('xcx_ads')
            ->join('advertise_5x', 'LEFT')
            ->on('xcx_ads.prefix', '=', DB::expr('concat(\'s_\',sline_advertise_5x.prefix)'))
            ->on('xcx_ads.number', '=', DB::expr('sline_advertise_5x.number'))
            ->where('xcx_ads.prefix', '=', $prefix)
            ->and_where('advertise_5x.is_show', '=', '1')
            ->and_where('advertise_5x.is_xcx', '=', 1)
            ->and_where(DB::expr(" AND FIND_IN_SET('{$client_no}',sline_xcx_ads.client_no) > 0"));
        if ($custom_label) {
            $builder->and_where('xcx_ads.custom_label', '=', $custom_label);
        }
		if($number){
            $builder->and_where('xcx_ads.number', '=', $number);
        }
        $ads = $builder->order_by(DB::expr('IFNULL(sline_xcx_ads.displayorder,9999)'), 'ASC')
            ->execute()
            ->as_array();
        foreach ($ads as &$item) {
            $item['adsrc'] = $this->img_url($item['adsrc']);
        }

        return $ads;
    }

    public function template_no($params): array
    {
        $page_name = Arr::get($params, 'pagename', '');
        $builder   = DB::select(
            'm_page_config.path',
            'page.pagename',
            'page.kindname'
        )
            ->from('m_page_config')
            ->join('page', 'inner')
            ->on('m_page_config.pageid', '=', DB::expr('sline_page.id'))
            ->where('m_page_config.isuse', '=', 1);

        if ($page_name) {
            $builder->and_where('page.pagename', '=', $page_name);
        } else {
            $builder->and_where('page.pagename', 'IS NOT', NULL)
                ->and_where('page.kindname', '!=', '');
        }

        $data = $builder->execute()->as_array();

        $path_arr = array();
        $tem_arr  = array();
        if (!empty($data)) {
            foreach ($data as $v) {
                $tem_arr[]                = $v['path'];
                $path_arr[$v['pagename']] = $v;
            }
        }

        return array(
            'path' => $path_arr,
            'tem' => $tem_arr,
        );
    }

    public function exchange_rate(): string
    {
        return Currency_Tool::symbol();
    }

    public function consulting($params): array
    {
        if($params['type_id']>199){
            $str = $params['type_id'];
        }else{
            $table = St_Functions::table_field($params['type_id']);
            $str = $table[3];
        }
        $is_open = DB::select()->from('sysconfig')->where('varname', '=', 'cfg_'. $str . '_consulting_is_open')->execute()->get('value');
        $qr_code = DB::select()->from('sysconfig')->where('varname', '=', 'cfg_'. $str . '_consulting_code')->execute()->get('value');
        return [
            'qr_code' => $this->img_url($qr_code),
            'is_open' => $is_open?:0,
        ];
    }

    use Trait_Api_Image;
}